/* 
 * @Author: 21克的爱情
 * @Date: 2020-10-04 13:20:49
 * @Email: raohong07@163.com
 * @LastEditors: 21克的爱情
 * @LastEditTime: 2020-10-05 23:31:08
 * @Description: 
 */

import 'package:sqflite/sqflite.dart';

import 'SQliteMessage.dart';

class SQliteHelp{
  SQliteMessage _sql;

  static final SQliteHelp _instance = new SQliteHelp.internal();
  static SQliteHelp get instance => _instance;
  SQliteHelp.internal(){
    _sql = SQliteMessage();
  }

  Future close() async {
    var result = _sql.close();
    return result;
  }

  /* 
   * @description: 新增或者更新数据
   * @param {type} 搜索名称
   * @return {type} 返回
   */
  Future<int> searchVideoName(String name)async{
    Database dbClient = await _sql.db;
    List<Map> hasList = await dbClient.rawQuery("SELECT * FROM ${SQliteMessage.tableSearch} WHERE name=\"$name\"");
    print(hasList);

    int result = 0;
    if(hasList.length > 0){
      Map<String, dynamic> upDate = Map<String, dynamic>.from(hasList.first);
      int count = upDate['counts'];
      count++;
      upDate['updateTime'] = DateTime.now().millisecondsSinceEpoch;
      upDate['counts'] = count;

      result = await dbClient.update(SQliteMessage.tableSearch, upDate, where: "name = ?", whereArgs: [name]);
      // dbClient.rawUpdate('UPDATE ${SQliteMessage.tableSearch} SET name = ?, value = ? WHERE name = ?', [name, '9876', 'some name'])
      // result = await dbClient.rawUpdate('UPDATE ${SQliteMessage.tableSearch} SET counts = ?, updateTime = ?, where name= ?', [upDate['counts'], upDate['updateTime'], name]);
    } else {
      Map<String, dynamic> inserDdata = {
        'name': name,
        'counts': 1,
        'updateTime': DateTime.now().millisecondsSinceEpoch,
        'createTime': DateTime.now().millisecondsSinceEpoch,
      };
      result = await dbClient.insert(SQliteMessage.tableSearch, inserDdata);
    }
    close();
    return result;
  }

  /* 
   * @description: 新增和更新播放记录
   * @param {type} 播放视频信息
   * @return {type} 返回
   */
  Future<int> playVideoData(Map<String, dynamic> upDate)async{
    Database dbClient = await _sql.db;
    List<Map> hasList = await dbClient.rawQuery("SELECT * FROM ${SQliteMessage.tablePlayData} WHERE vod_id=\"${upDate['vod_id']}\"");
    print(hasList);

    int result = 0;
    if(hasList.length > 0){
      Map<String, dynamic> searchDate = Map<String, dynamic>.from(hasList.first);
      searchDate['updateTime'] = DateTime.now().millisecondsSinceEpoch;
      searchDate.addAll(upDate);
      
      result = await dbClient.update(SQliteMessage.tablePlayData, searchDate, where: "vod_id = ?", whereArgs: [upDate['vod_id']]);
      // dbClient.rawUpdate('UPDATE ${SQliteMessage.tablePlayData} SET name = ?, value = ? WHERE name = ?', [name, '9876', 'some name'])
      // result = await dbClient.rawUpdate('UPDATE ${SQliteMessage.tablePlayData} SET counts = ?, updateTime = ?, where name= ?', [upDate['counts'], upDate['updateTime'], name]);
    } else {
      upDate['createTime'] = DateTime.now().millisecondsSinceEpoch;
      result = await dbClient.insert(SQliteMessage.tablePlayData, upDate);
    }
    close();
    return result;
  }

  /* 
  * @description: 获取数据库数据
  * @param {type} null
  * @return {type} List
  */  
  Future<List<Map<String, dynamic>>> searchData( String surface ) async {
    Database dbClient = await _sql.db;
    List<Map<String, dynamic>> list = await dbClient.query(surface);
    return list;
  }

  /* 
   * @description: 清空表中的数据
   * @param {type} null
   * @return {type} 
   */
  Future<void> cleanData( String surface ) async {
    Database dbClient = await _sql.db;
    //先删除
    int deleteCount = await dbClient.rawDelete("DELETE FROM $surface");
    print("deleteCount:$deleteCount");
  }

  /* 
   * @description: 清空表中的数据
   * @param {type} null
   * @return {type} 
   */
  get deleteTable async {
    Database dbClient = await _sql.db;
    await deleteDatabase(dbClient.path);
    print('数据库删除成功');
  }

}